﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LDTB.Service;

namespace LDTB
{
    public partial class CatOptEditFrm : Form
    {
        LDTBDataDataContext ds = DataService.DB;
        int id;


        public CatOptEditFrm()
        {
            InitializeComponent();
        }

        public CatOptEditFrm(int id_) {
            id = id_;
            InitializeComponent();
        }

        private void CatOptEditFrm_Load(object sender, EventArgs e)
        {

            var cat = from c in ds.CateObjects
                      select c;

            ddl.DataSource = cat;
            ddl.DisplayMember = "CatName";
            ddl.ValueMember = "CatID";


            var catop = (from ct in ds.OptionCates
                        where ct.ID==id
                        select ct).FirstOrDefault();

            this.textBox1.Text = catop.DegreeName;
            this.ddl.SelectedValue = catop.CateDisableID;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string name = this.textBox1.Text.Trim();
            int id=int.Parse(this.ddl.SelectedValue.ToString());

            if (name != null)
            {
                var catop = (from c in ds.OptionCates
                            where c.ID == id
                            select c).FirstOrDefault();

                if (catop != null) {

                    catop.DegreeName = name;
                    catop.CateDisableID = id;
                    ds.SubmitChanges();

                    MessageBox.Show("Cập nhật thành công");

                } else {

                    MessageBox.Show("Không tồn tại ");
                
                }
            }
            else {

                MessageBox.Show("Nhập tên thể loại");
            }

        }
    }
}
